package com.mycode.interpreter16.resolve2;

import java.util.StringTokenizer;

// 环境类：用于存储和操作需要解释的语句
// 标记 或 命令
public class Context {

    // StringTokenizer 用于将字符串分解为更小的字符传标记，默认情况下以空格作为分隔符
    private StringTokenizer tokenizer;
    //当前字符串标记
    private String currentToken;

    public Context(String text) {
        this.tokenizer = new StringTokenizer(text);
        nextToken();
    }

    // 返回下一个标记
    public String nextToken() {
        if (tokenizer.hasMoreTokens()) {
            currentToken = tokenizer.nextToken();
        }
        else {
            currentToken = null;
        }
        return currentToken;
    }

    // 返回当前的标记
    public String currentToken() {
        return currentToken;
    }

    // 跳过一个标记
    public void skipToken(String token) {
        if (!token.equals(currentToken)) {
            System.out.println("错误提示：" + currentToken + "解释错误！");
        }
        nextToken();
    }

    // 如果当前的标记是一个数字，则返回对应的数值
    public int currentNumber() {
        int number = 0;
        try {
            number = Integer.parseInt(currentToken);
        } catch (NumberFormatException e) {
            System.out.println("错误提示：" + e);
        }
        return number;
    }
}
